package com.cskaoyan.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.user.converter.UserConverterMapper;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.form.UserLoginRequest;
import com.cskaoyan.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : qj
 * @since: 2022/10/26  10:52
 */
@Service

public class LoginServiceImpl implements ILoginService {


    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private UserConverterMapper userConverterMapper;
    @Override
    public UserLoginResponse login(UserLoginRequest request) {
        UserLoginResponse response = new UserLoginResponse();
        //验证用户名和密码
        request.requestCheck();
        Example example = new Example(Member.class);
        example.createCriteria()
                .andEqualTo("username",request.getUserName());
        List<Member> members = memberMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(members)){
            response.setCode(SysRetCodeConstants.USERORPASSWORD_ERROR.getCode());
            response.setMsg(SysRetCodeConstants.USERORPASSWORD_ERROR.getMessage());
            return response;
        }

        Member member = members.get(0);

        //TODO 判断用户是否已经激活
        if (!"Y".equals( member.getIsVerified())){
            response.setCode(SysRetCodeConstants.USER_NO_VERIFIED.getCode());
            response.setMsg(SysRetCodeConstants.USER_NO_VERIFIED.getMessage());
            return response;
        }
        //判断用户名和密码是否正确
        String userPwd = request.getUserPwd();
        String md5Password = DigestUtils.md5DigestAsHex(userPwd.getBytes());
        if (!md5Password.equals(member.getPassword())){
            response.setCode(SysRetCodeConstants.USERORPASSWORD_ERROR.getCode());
            response.setMsg(SysRetCodeConstants.USERORPASSWORD_ERROR.getMessage());
            return response;
        }

        //产生JWT token
        Map map = new HashMap<>();
        map.put("uid",member.getId());
        map.put("file",member.getFile());
        map.put("username",member.getUsername());
        String token = JwtTokenUtils.builder().msg(JSON.toJSONString(map)).build().creatJwtToken();

        response = userConverterMapper.converter(member);
        response.setToken(token);
        response.setCode(SysRetCodeConstants.SUCCESS.getCode());
        response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return response;
    }
}
